//
//  PTMineViewController.swift
//  PropertyClient
//
//  Created by benwang on 2021/6/22.
//

import UIKit
import RxSwift
import RxDataSources
import SnapKit
import CollectionKit

class PTMineViewController: UIViewController {

  var collectionView = CollectionView()

  lazy var headerView = R.nib.ptMineHeaderView(owner: self)!

  let disposeBag = DisposeBag()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    setupTreeView()

    bindingObserver()
  }


  /*
   // MARK: - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   // Get the new view controller using segue.destination.
   // Pass the selected object to the new view controller.
   }
   */

}

extension PTMineViewController {

  func setupTreeView() {

    opacityNavigationBar()

    view.backgroundColor = UIColor(hex: 0xFAFAFA)
    view.addSubview(collectionView)

    collectionView.snp.makeConstraints { maker in
      maker.edges.equalToSuperview()
    }
  }

  func bindingObserver() {

    title = "我的"

    headerView.eidtButton.rx.tap.subscribe(onNext: {
      let vc = ProfileEditViewController()
      vc.hidesBottomBarWhenPushed = true
      self.navigationController?.pushViewController(vc, animated: true)
    })
    .disposed(by: disposeBag)

    let headerProvider = SimpleViewProvider(
        views: [headerView],
        sizeStrategy: (.fill, .absolute(230))
    )

    let dataSource = ArrayDataSource<String>(data:[
      "我的通知",
      "修改密码",
      "关于"
    ])

    let viewSource = ClosureViewSource<String, PTMineItemView> { item, text, index in
      item.label.text = text
    }

    let sizeSource = { (index: Int, data: String, size: CGSize) in
        return CGSize(width: size.width - 20, height: 58)
    }

    let itemProvider = BasicProvider<String, PTMineItemView>(
      dataSource: dataSource,
      viewSource: viewSource,
      sizeSource: sizeSource,
      layout: FlowLayout(lineSpacing: 15, justifyContent: .center)
    )

    itemProvider.tapHandler = { (context) in
      self.jump(to: context.index)
    }

    collectionView.provider = ComposedProvider(
        layout: FlowLayout(lineSpacing:32),
        sections: [headerProvider,itemProvider])
    
  }

  func jump(to index: Int) {
    if index == 0 {
      let vc = NoticeBilboardViewController()
      vc.hidesBottomBarWhenPushed = true
      navigationController?.pushViewController(vc, animated: true)
    } else {

    }
  }
}
